package com.pds.web.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.web.area.entity.Area;
import com.pds.web.area.mapper.AreaMapper;
import com.pds.web.area.service.AreaService;
import com.pds.web.area.vo.AreaCityVO;
import com.pds.web.area.vo.AreaCountyVO;
import com.pds.web.area.vo.AreaProvinceVO;
import com.pds.web.area.vo.AreaTownVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 位置地区表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-03-15
 */
@Service("areaService")
public class AreaServiceImpl extends ServiceImpl<AreaMapper,Area> implements AreaService {
    @Resource
    private AreaMapper areaMapper;

    @Override
    public List<AreaProvinceVO> selectList(Area area) {
        List<AreaProvinceVO> list = new ArrayList<>();
        QueryWrapper<Area> wrapperProvince = new QueryWrapper<>();
        wrapperProvince.eq("level",1);
        List<Area> areaProvince = areaMapper.selectList(wrapperProvince);
        if(!Objects.isNull(areaProvince) && !areaProvince.isEmpty()){
            for (Area province : areaProvince) {
                String code = province.getCode();
                AreaProvinceVO provinceVO = new AreaProvinceVO();
                BeanUtils.copyProperties(province,provinceVO);
                List<AreaCityVO> cityList = queryCityList(code);
                provinceVO.setChildren(cityList);
                list.add(provinceVO);
            }
        }
        return list;
    }

    private List<AreaCityVO> queryCityList(String parentCode){
        List<AreaCityVO> cityList = new ArrayList<>();
        QueryWrapper<Area> wrapperCity = new QueryWrapper<>();
        wrapperCity.eq("parent_code",parentCode);
        wrapperCity.eq("level",2);
        List<Area> areaCity = areaMapper.selectList(wrapperCity);
        if(!Objects.isNull(areaCity) && !areaCity.isEmpty()){
            for (Area city : areaCity) {
                String code = city.getCode();
                AreaCityVO cityVO = new AreaCityVO();
                BeanUtils.copyProperties(city,cityVO);
                List<AreaCountyVO> countyList = queryCountyList(code);
                cityVO.setChildren(countyList);
                cityList.add(cityVO);
            }
        }
        return cityList;
    }

    private List<AreaCountyVO> queryCountyList(String parentCode){
        List<AreaCountyVO> countyList = new ArrayList<>();
        QueryWrapper<Area> wrapperCounty = new QueryWrapper<>();
        wrapperCounty.eq("parent_code",parentCode);
        wrapperCounty.eq("level",3);
        List<Area> areaCounty = areaMapper.selectList(wrapperCounty);
        if(!Objects.isNull(areaCounty) && !areaCounty.isEmpty()){
            for (Area county : areaCounty) {
                String code = county.getCode();
                AreaCountyVO countyVO = new AreaCountyVO();
                BeanUtils.copyProperties(county,countyVO);
                List<AreaTownVO> townList = queryTownList(code);
                countyVO.setChildren(townList);
                countyList.add(countyVO);
            }
        }
        return countyList;
    }

    private List<AreaTownVO> queryTownList(String parentCode){
        List<AreaTownVO> townList = new ArrayList<>();
        QueryWrapper<Area> wrapperTown = new QueryWrapper<>();
        wrapperTown.eq("parent_code",parentCode);
        wrapperTown.eq("level",4);
        List<Area> areaTown = areaMapper.selectList(wrapperTown);
        if(!Objects.isNull(areaTown) && !areaTown.isEmpty()){
            for (Area town : areaTown) {
                AreaTownVO townVO = new AreaTownVO();
                BeanUtils.copyProperties(town,townVO);
                townList.add(townVO);
            }
        }
        return townList;
    }

    @Override
    public List<AreaProvinceVO> queryArea() {
        return areaMapper.queryArea();
    }

    /**
     * 根据等级查询行政地区列表
     *
     * @param level 等级 1省、2市、3区县、4街道
     * @return
     */
    @Override
    public List<AreaProvinceVO> listByLevel(Integer level) {
        return areaMapper.listByLevel(level);
    }
}
